/*********************************************************
*Copyright (c) 2015 zhongsx. All Rights in Pw 
*@des: 环型缓冲区 
*@name: CircleBuffer.h
*@author: zhongsx
*@time: 2015-03-07
*********************************************************/
#ifndef __CIRCLE_BUFFER_H__
#define __CIRCLE_BUFFER_H__

#include <stdio.h>
#include <stdlib.h>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>

typedef unsigned char           uint8_t;

/*
 * 环形缓冲区
 */
class CircleBuffer {
private:
    uint8_t* buffer_;
    int read;   //读取数据 index
    int write;  //写数据 index
    int max_size; //buffer 最大容量
    mutable boost::recursive_mutex   mutex_;

public:
    CircleBuffer(int MaxSize = 10240);
    ~CircleBuffer();
 
    int write_data(uint8_t* elem, int wsize);
	//当type == 0 时 实际读取rsize个字节 计数
	//type != 0 时 预先读取rsize个字节 不计数
	int read_data(uint8_t* elem, int rsize, int msize, int type=0);
    
    void clear();
    bool empty();
    int  cont(); 
    int  emptysize();
};
 
#endif //__CIRCLE_BUFFER_H__